package com.lw.behavioral.memento;

public class MementoPatternDemo {
   public static void main(String[] args) {
      /**
       * Originator 创建并在 Memento 对象中存储状态。
       */
      Originator originator = new Originator();
      /**
       * Caretaker 对象负责从 Memento 中恢复对象的状态。
       */
      CareTaker careTaker = new CareTaker();

      originator.setState("State #1");//设置状态
      originator.setState("State #2");

      careTaker.add(originator.saveStateToMemento());//保存状态到Memento
      originator.setState("State #3");
      careTaker.add(originator.saveStateToMemento());
      originator.setState("State #4");
 
      System.out.println("Current State: " + originator.getState()); //获取状态
      originator.getStateFromMemento(careTaker.get(0));//恢复状态
      System.out.println("First saved State: " + originator.getState());
      originator.getStateFromMemento(careTaker.get(1));
      System.out.println("Second saved State: " + originator.getState());
   }
}